<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Simades extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_sys() {
        $get = $this->db->get('sys');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function login($log) {
        $this->db->where('username', $log['username']);
        $this->db->where('password', $log['password']);
        $data = $this->db->get('user');
        if ($data->num_rows == 1) {
            return $data->row();
        } else {
            return FALSE;
        }
    }

    function get_kewarganegaraan() {
        $get = $this->db->get('kewarganegaraan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function agama_add($agama) {
        $this->db->insert('agama', $agama);
    }

    function get_agama() {
        $get = $this->db->get('agama');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_agama_id($id) {
        $this->db->where('id', $id);
        $get = $this->db->get('agama');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function agama_update($agama) {
        $this->db->where('id', $agama['id']);
        $this->db->update('agama', $agama);
    }

    function agama_del($id) {
        $this->db->where('id', $id);
        $this->db->delete('agama');
    }

    function get_pekerjaan($config) {
        $get = $this->db->get('pekerjaan',$config['per_page'],  $config['uri']);
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_pekerjaan_id($id) {
        $this->db->where('id', $id);
        $get = $this->db->get('pekerjaan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function pekerjaan_add($pekerjaan) {
        $this->db->insert('pekerjaan', $pekerjaan);
    }

    function pekerjaan_update($pekerjaan) {
        $this->db->where('id', $pekerjaan['id']);
        $this->db->update('pekerjaan', $pekerjaan);
    }

    function pekerjaan_del($id) {
        $this->db->where('id', $id);
        $this->db->delete('pekerjaan');
    }

    function get_pendidikan() {
        $get = $this->db->get('pendidikan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function pendidikan_add($pendidikan) {
        $this->db->insert('pendidikan', $pendidikan);
    }

    function pendidikan_update($pendidikan) {
        $this->db->where('id', $pendidikan['id']);
        $this->db->update('pendidikan', $pendidikan);
    }

    function pendidikan_del($id) {
        $this->db->where('id', $id);
        $this->db->delete('pendidikan');
    }

    function get_propinsi() {
        $get = $this->db->get('propinsi');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_id_propinsi() {
        $this->db->select('id');
        $get = $this->db->get('propinsi');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_kabupaten() {
        $get = $this->db->get('kabupaten');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_id_kabupaten() {
        $this->db->select('id');
        $get = $this->db->get('kabupaten');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_kecamatan() {
        $get = $this->db->get('kecamatan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_id_kecamatan() {
        $this->db->select('id');
        $get = $this->db->get('kecamatan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_dusun() {
        $this->db->select('id,keterangan');
        $get = $this->db->get('dusun');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_kk() {
        $get = $this->db->get('vget_kk');
        if ($get->num_rows > 0) {
            return $get->result();
        } else {
            return null;
        }
    }

    function get_last_kk_id($now) {
        $this->db->select_max('id');
        $this->db->where('dibuat', $now);
        $kk = $this->db->get('kk');
        if ($kk->num_rows > 0) {
            return $kk->result();
        }
    }

    function kk_add($kk) {
        $this->db->insert('kk', $kk);
    }

    function kk_del($id) {
        $this->db->where('id', $id);
        $this->db->delete('kk');
    }

    function get_pndk() {
        $this->db->order_by("kk_id,hub_id", "asc");
        $get = $this->db->get('vget_nik');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_pndk_nik($nik) {
        $this->db->where('nik', $nik);
        $get = $this->db->get('vget_nik');
        if ($get->num_rows > 0) {
            return $get->result();
        } else {
            return $get->result();
        }
    }

    function get_last_nik($tmp) {
        $this->db->select_max('nik');
        $this->db->where('tgl_lahir', $tmp);
        $nik = $this->db->get('penduduk');
        if ($nik->num_rows > 0) {
            return $nik->result();
        } else {
            return $nik = 0;
        }
    }

    function get_pndk_kk($kk) {
        $this->db->where('kk_id', $kk);
        $this->db->order_by("hub_id", "asc");
        $this->db->order_by("tgl_lahir", "asc");
        $get = $this->db->get('vget_nik');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function cek_nik($new_nik) {
        $this->db->where('nik', $new_nik);
        $isi = $this->db->get('penduduk');
        if ($isi->num_rows > 0) {
            return $isi->result();
        }
    }

    function del_last_nik($new_nik) {
        $this->db->where('nik', $new_nik);
        $this->db->delete('kk');
    }

    function get_sex() {
        $get = $this->db->get('jenis_kelamin');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function sex_add($sex){
        $this->db->insert('jenis_kelamin', $sex);
    }
    
    function sex_update($sex){
        $this->db->where('id', $sex['id']);
        $this->db->update('jenis_kelamin',$sex);
    }
    
    function sex_del($id){
        $this->db->where('id',$id);
        $this->db->delete('jenis_kelamin');
    }


    function get_darah() {
        $get = $this->db->get('gol_darah');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_cacat() {
        $get = $this->db->get('cacat');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_perkawinan() {
        $get = $this->db->get('perkawinan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_hubungan() {
        $get = $this->db->get('hubungan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_warga() {
        $get = $this->db->get('kewarganegaraan');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function nik_add($nik) {
        $this->db->insert('penduduk', $nik);
    }

    function nik_new($nik) {
        $this->db->insert('penduduk', $nik);
    }

    // Fungsi delete
    function del_nik($nik) {
        $this->db->where('nik', $nik);
        $this->db->delete('penduduk');
    }

    function cari($id) {
        $get = $this->db->query("select nik,kk_id,nama from penduduk where nik = '" . $id . "' or kk_id = '" . $id . "' or nama like '%" . $id . "%'");
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    /* System area */

    function get_genset() {
        $get = $this->db->get('vget_genset');
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

    function get_pek() {
        $get = $this->db->get('pekerjaan');
        if ($get->num_rows > 0) {
            return $get->num_rows();
        }
    }
    
    function get_peker($config) {
        $get = $this->db->get('pekerjaan',$config['per_page'],  $config['uri']);
        if ($get->num_rows > 0) {
            return $get->result();
        }
    }

}

?>